using UnityEngine;
using Delta2DGame.Base;

namespace Delta2DGame.State
{
    /// <summary>
    /// 跑步状态
    /// </summary>
    public class RunState : PlayerState
    {
        public RunState(PlayerStateMachine stateMachine, PlayerController player, string animBoolName) : base(stateMachine, player, animBoolName)
        {}

        public override void Update()
        {
            base.Update();
            FlipWithPlayer();//角色翻转

            // 检测移动输入为0，切换到待机状态
            if (Mathf.Abs(player.GetMoveInput().x) < 0.1f && Mathf.Abs(player.GetMoveInput().y) < 0.1f)
            {
                stateMachine.SwitchState(stateMachine.idleState);
            }
            // 检测跳跃输入，切换到跳跃状态
            else if (player.IsJumpPressed())
            {
                stateMachine.SwitchState(stateMachine.jumpState);
            }
        }

        // public override void FixedUpdate()
        // {
        //     base.FixedUpdate();
        //     // 应用移动
        //     // player.Move(player.GetMoveInput());
        // }
        
        private void FlipWithPlayer()
        {
            //角色翻转 - 只翻转PlayerSkeletonAnimation
            if (skeletonTransform != null)
            {
                if (player.GetMoveInput().x < 0)
                {
                    skeletonTransform.localScale = new Vector3(-Mathf.Abs(skeletonTransform.localScale.x), skeletonTransform.localScale.y, skeletonTransform.localScale.z);
                }
                else if (player.GetMoveInput().x > 0)
                {
                    skeletonTransform.localScale = new Vector3(Mathf.Abs(skeletonTransform.localScale.x), skeletonTransform.localScale.y, skeletonTransform.localScale.z);
                }
            }

            // animator.SetFloat("Speed", movement.magnitude);//播放动画
        }
    }
}